<?
/**
 * Copyright 2008 Melange.
 *
 * This file is part of Melange CMS
 *
 * @package cms
 *
 */


/**
 * Requirements
 */
require_once("mvc/action/ActionForm.php");
require_once("mvc/action/ActionMessage.php");
require_once("mvc/action/ActionMessages.php");
require_once("cms/menu/om/MenuItemI18n.php");



/**
 * BaseItemForm Class
 *
 * @category    Melange CMS
 * @package     cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 */
class BaseItemForm extends ActionForm {


    // ----------------------------------------------------- Instance Variables


    /**
     * itemI18n
     *
     * @var MenuItemI18n 
     */
    protected $itemI18n;


    /**
     * internalLink
     *
     * @var mixed 
     */
    protected $internalLink;


    /**
     * save
     *
     * @var mixed 
     */
    protected $save;


    /**
     * module
     *
     * @var mixed 
     */
    protected $module;


    /**
     * options
     *
     * @var mixed 
     */
    protected $options;


    /**
     * formThumbnail
     *
     * @var mixed 
     */
    protected $formThumbnail;


    /**
     * deleteThumbnail
     *
     * @var mixed 
     */
    protected $deleteThumbnail;


    /**
     * formImage
     *
     * @var mixed 
     */
    protected $formImage;


    /**
     * deleteImage
     *
     * @var mixed 
     */
    protected $deleteImage;


    // ----------------------------------------------------------- Constructors


    /**
     * Construct new BaseItemForm
     */
    public function __construct() {
        $this->itemI18n = new MenuItemI18n();
        $this->internalLink = null;
        $this->save = null;
        $this->module = null;
        $this->options = null;
        $this->formThumbnail = null;
        $this->deleteThumbnail = false;
        $this->formImage = null;
        $this->deleteImage = false;
    }


    // ------------------------------------------------------------- Properties


    /**
     * Get ItemI18n
     *
     * @return MenuItemI18n 
     */
    public function getItemI18n() {
        return $this->itemI18n;
    }


    /**
     * Set ItemI18n
     *
     * @param MenuItemI18n  itemI18n
     */
    public function setItemI18n(MenuItemI18n $itemI18n) {
        $this->itemI18n = $itemI18n;
    }


    /**
     * Get InternalLink
     *
     * @return mixed 
     */
    public function getInternalLink() {
        return $this->internalLink;
    }


    /**
     * Set InternalLink
     *
     * @param mixed  internalLink
     */
    public function setInternalLink($internalLink) {
        $this->internalLink = $internalLink;
    }


    /**
     * Get Save
     *
     * @return mixed 
     */
    public function getSave() {
        return $this->save;
    }


    /**
     * Set Save
     *
     * @param mixed  save
     */
    public function setSave($save) {
        $this->save = $save;
    }


    /**
     * Get Module
     *
     * @return mixed 
     */
    public function getModule() {
        return $this->module;
    }


    /**
     * Set Module
     *
     * @param mixed  module
     */
    public function setModule($module) {
        $this->module = $module;
    }


    /**
     * Get Options
     *
     * @return mixed 
     */
    public function getOptions() {
        return $this->options;
    }


    /**
     * Set Options
     *
     * @param mixed  options
     */
    public function setOptions($options) {
        $this->options = $options;
    }


    /**
     * Get FormThumbnail
     *
     * @return mixed 
     */
    public function getFormThumbnail() {
        return $this->formThumbnail;
    }


    /**
     * Set FormThumbnail
     *
     * @param mixed  formThumbnail
     */
    public function setFormThumbnail($formThumbnail) {
        $this->formThumbnail = $formThumbnail;
    }


    /**
     * Get DeleteThumbnail
     *
     * @return mixed 
     */
    public function getDeleteThumbnail() {
        return $this->deleteThumbnail;
    }


    /**
     * Set DeleteThumbnail
     *
     * @param mixed  deleteThumbnail
     */
    public function setDeleteThumbnail($deleteThumbnail) {
        $this->deleteThumbnail = $deleteThumbnail;
    }


    /**
     * Get FormImage
     *
     * @return mixed 
     */
    public function getFormImage() {
        return $this->formImage;
    }


    /**
     * Set FormImage
     *
     * @param mixed  formImage
     */
    public function setFormImage($formImage) {
        $this->formImage = $formImage;
    }


    /**
     * Get DeleteImage
     *
     * @return mixed 
     */
    public function getDeleteImage() {
        return $this->deleteImage;
    }


    /**
     * Set DeleteImage
     *
     * @param mixed  deleteImage
     */
    public function setDeleteImage($deleteImage) {
        $this->deleteImage = $deleteImage;
    }


    // --------------------------------------------------------- Public Methods


    /**
     * Validate this form
     *
     * @return array $errors
     */
    public function validate() {

        $errors = new ActionMessages();

        if(is_null($this->getItemI18n()->getMenuItem()->getMenuId()) || strlen($this->getItemI18n()->getMenuItem()->getMenuId()) == 0)
            $errors->add("itemI18n->menuItem->menuId",
                new ActionMessage("errors.required", true, new ActionMessage("Menu Id", false)));


        if(!is_null($this->getItemI18n()->getMenuItem()->getMenuId()) && strlen($this->getItemI18n()->getMenuItem()->getMenuId()) > 0 && strlen($this->getItemI18n()->getMenuItem()->getMenuId()) > 255)
            $errors->add("itemI18n->menuItem->menuId",
                new ActionMessage("errors.maxlength", true, array(
                    new ActionMessage("Menu Id", false),
                    new ActionMessage("255", false))));


        if(!is_null($this->getItemI18n()->getMenuItem()->getLink()) && strlen($this->getItemI18n()->getMenuItem()->getLink()) > 0 && strlen($this->getItemI18n()->getMenuItem()->getLink()) > 255)
            $errors->add("itemI18n->menuItem->link",
                new ActionMessage("errors.maxlength", true, array(
                    new ActionMessage("prompt.item.link", true),
                    new ActionMessage("255", false))));


        if(!is_null($this->getItemI18n()->getMenuItem()->getLink()) && strlen($this->getItemI18n()->getMenuItem()->getLink()) > 0 && !preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $this->getItemI18n()->getMenuItem()->getLink()))
            $errors->add("itemI18n->menuItem->link",
                new ActionMessage("errors.url", true, new ActionMessage("prompt.item.link", true)));


        global $ALLOWABLE_IMAGES;
        if(!is_null($this->getFormThumbnail()) && isset($this->formThumbnail["size"]) && $this->formThumbnail["size"] > 0 && !array_key_exists($this->formThumbnail["type"], $ALLOWABLE_IMAGES))
            $errors->add("formThumbnail",
                new ActionMessage("errors.image", true, new ActionMessage("prompt.thumbnail", true)));

        global $ALLOWABLE_IMAGES;
        if(!is_null($this->getFormImage()) && isset($this->formImage["size"]) && $this->formImage["size"] > 0 && !array_key_exists($this->formImage["type"], $ALLOWABLE_IMAGES))
            $errors->add("formImage",
                new ActionMessage("errors.image", true, new ActionMessage("prompt.image", true)));

        return $errors;
    }

}
?>